#include <stdio.h>
#include <fcntl.h>
#if !defined( unix )
#include <io.h>
#endif

int main( int argc, char *argv[] ){
    fprintf( stderr, "Run length decoding " );
    if ( argc > 1 ) {
        freopen( argv[ 1 ], "rb", stdin );
        fprintf( stderr, "%s", argv[ 1 ] );
    }else{
        fprintf( stderr, "stdin" );
    }
    fprintf( stderr, " to " );
    if ( argc > 2 ){
        freopen( argv[ 2 ], "wb", stdout );
        fprintf( stderr, "%s", argv[ 2 ] );
    }else{
        fprintf( stderr, "stdout" );
    }
    fprintf( stderr, "\n" );
    
#if !defined( unix )
    setmode( fileno( stdin ), O_BINARY );
    setmode( fileno( stdout ), O_BINARY );
#endif

    short int last_1 = 1;
    short int last_2 = 2;
    short int last_3 = 3;
    short int c;
    short int count;
    while ( ( c = getc( stdin ) ) >= 0 ){
        putc( c, stdout );
        if ( ( c == last_1 ) && ( c == last_2 ) && ( c == last_3 ) ) {
            count = getc( stdin );
            while ( count-- > 0 )
                putc( c, stdout );
        }
        last_3 = last_2;
        last_2 = last_1;
        last_1 = c;
    }
    return 0;
}
